package com.snailct.bpp.db.template;



import com.snailct.bpp.db.MyBatisPojo;
import  org.apache.ibatis.jdbc.SQL;

import java.util.Map;

import static org.apache.ibatis.jdbc.SqlBuilder.*;


/**
 * Created by huangxa on 2016/7/20.
 */
public class CUDTemplate<T extends MyBatisPojo> {
    public String insert(T obj) {
        BEGIN();
        INSERT_INTO(obj.tablename());
        obj.caculationColumnList();
        VALUES(obj.returnInsertColumnsName(), obj.returnInsertColumnsDefine());
        return SQL();
    }
    public String getSql(T obj,Map<String, Object> parameters){

        BEGIN();
        SELECT(obj.returnInsertColumnsName());
        FROM(obj.tablename());
        if (parameters != null) {
            WHERE(obj.returnInsertColumnsDefine());
        }
        return SQL();
    }
    public String get(T obj) {

        BEGIN();
        INSERT_INTO(obj.tablename());
        obj.caculationColumnList();
        VALUES(obj.returnInsertColumnsName(), obj.returnInsertColumnsDefine());
        return SQL();
    }


    public String select() {
        BEGIN();
        SELECT("*");
        FROM("bt_user");
        return SQL();
    }

    public String getById(T obj){
        BEGIN();
        SELECT("*");
        FROM(obj.tablename());
        WHERE("id=#{ id}");
        return SQL();
    }

    public String delete(T obj) {
        Integer id = obj.getId();

        BEGIN();

        DELETE_FROM(obj.tablename());
        WHERE("id=#{ id}");

        return SQL();
    }

    public String update(T obj) {
        String idname = obj.id();

        BEGIN();

        UPDATE(obj.tablename());
        obj.caculationColumnList();
        SET(obj.returnUpdateSet());
        WHERE(idname + "=#{" + idname + "}");

        return SQL();
    }


}
